package com.iwhalecloud.bss.kite.cucc.web.service.accept.util;

import com.iwhalecloud.bss.kite.client.vo.spec.ServiceOfferVO;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.ServiceOfferMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.ztesoft.bss.common.util.SpringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @Author deng.gang
 * @Date 2021/9/18 9:54 上午
 */
public class CommonInfoUtils {

    public static ServiceOfferVO getServiceOfferVO(String sceneInstId, String custId, String offerId) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String serviceOfferId = querySceneInstService.getSceneInst(sceneInstId).getServiceOfferId();
        // 如果是追加订购，需要将1转回2019进行获取
        if (KiteStringUtils.equals(querySceneInstService.getExtParameterValue(sceneInstId, LocalKeyConsts.ADD_ORDE), "T") && ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(serviceOfferId)) {
            serviceOfferId = CuccServiceOfferConsts.SERVICE_OFFER_ADD_ORDER;
        }
        String objId = OfferCache.get(offerId).getAttrValue("cateId");
        String prodId = "";
        KiteOfferInst giftOfferInst = querySceneInstService.getGiftOfferInst(sceneInstId);
        if (Objects.nonNull(giftOfferInst)) {
            List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(custId, giftOfferInst.getOfferInstId());
            for (KiteProdInst prodInst : prodInstList) {
                prodId = prodInst.getProdId();
            }
        } else {
            KiteOfferInst mainOfferInst = querySceneInstService.getMainOfferInst(sceneInstId);
            if (Objects.nonNull(mainOfferInst)) {
                List<KiteProdInst> prodInstList = querySceneInstService.listProdInstsFromOfferInst(custId, mainOfferInst.getOfferInstId());
                for (KiteProdInst prodInst : prodInstList) {
                    prodId = prodInst.getProdId();
                }
            }
        }
        ServiceOfferVO serviceOfferVO = getServiceOffer(serviceOfferId, objId);
        if (Objects.isNull(serviceOfferVO)) {
            serviceOfferVO = getServiceOffer(serviceOfferId, prodId);
        }
        return serviceOfferVO;
    }

    public static ServiceOfferVO getServiceOffer(String serviceOfferId, String cateId) {
        ServiceOfferMapper mapper = SpringUtil.getBean(ServiceOfferMapper.class);
        Map<String, Object> requestMap = new HashMap<>();
        requestMap.put("offerId", cateId);
        requestMap.put("serviceOfferId", serviceOfferId);
        return mapper.getOfferService(requestMap).stream().findFirst().orElse(null);
    }
}
